﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using KelonProud.Domain.Dtos;
using Microsoft.AspNetCore.Html;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.AspNetCore.Mvc.Routing;
using Microsoft.AspNetCore.Mvc.ViewFeatures.Internal;

namespace KelonProud.Mvc.Controls
{
    public static class AccessoryExtension
    {
        public static IHtmlContent AccessoryPanel(
            this IHtmlHelper helper,
            string name, 
            IEnumerable<AccessoryOutPut> accessoryList,
            string zipName ="",
            AccessoryPanelStyle style = AccessoryPanelStyle.Tile)
        {
            return AccessoryPanelHelper(helper, name, accessoryList,style,zipName);
        }

        public static IHtmlContent AccessoryPanelFor<TModel, TProperty>(
            this IHtmlHelper<TModel> helper,
            Expression<Func<TModel, TProperty>> expression,
            string zipName = "",
            AccessoryPanelStyle style = AccessoryPanelStyle.Tile)
        {
            var factory = new KelonProudControlHelper<TModel>().GetModelExplorer(helper, expression);
            var expressionText = ExpressionHelper.GetExpressionText(expression);
            var name = NameAndIdProvider.GetFullHtmlFieldName(helper.ViewContext, expressionText);
            var value = (IEnumerable<AccessoryOutPut>)factory.Model;


            return AccessoryPanelHelper(helper, name, value, style, zipName);
        }

        private static IHtmlContent AccessoryPanelHelper(
            this IHtmlHelper helper,
            string name,
            IEnumerable<AccessoryOutPut> accessoryList, 
            AccessoryPanelStyle style,
            string zipName)
        {
            if (string.IsNullOrEmpty(name))
            {
                throw new ArgumentException("name属性不能为空", nameof(name));
            }

            if (accessoryList==null)
            {
                throw new ArgumentException("accessoryList属性不能为空", nameof(accessoryList));
            }

            var urlHelper = new UrlHelper(helper.ViewContext);
            var htmlBuilder = new StringBuilder();
            var scriptBuilder = new StringBuilder();
            var accessoryCount = accessoryList.Count();
            var ids = accessoryList.Select(s => s.AccessoryId).Aggregate((a, b) => a +","+ b);
            zipName = zipName ?? DateTime.Now.ToString("yyyyMMdd-附件");

            htmlBuilder.Append("<div class=\"z-accessory-panel\" id=\"{name}\">" +
                               "<div class=\"z-accessory-title\">" +
                               " <i class=\"fa fa-paperclip\"></i>" +
                               " <span class=\"accessory-name\">附件</span>" +
                               "<span>({accessoryCount})</span>" );

            if (accessoryCount> 0)
            {
                htmlBuilder.AppendLine("<a  href=\"{batchAccessoryUrl}\" class=\"btn btn-link red-text\"> 打包下载</a>");
            }

            htmlBuilder.AppendLine("</div>");
            htmlBuilder.AppendLine("<ul class=\"z-accessory-list\">");

            accessoryList.Each(e =>
            {
                htmlBuilder.AppendLine(
                    " <li data-toggle=\"tooltip\" data-placement=\"bottom\" title=\"点击下载：{0}\">".FormatWith(e.FileName));
                htmlBuilder.AppendLine("<a class=\"z-accessory-item\" href=\"{0}\">".FormatWith(urlHelper.Action("GetAccessory", "Accessory", new { id = e.AccessoryId })));
                htmlBuilder.AppendLine("<span class=\"accessory-pic\"> <i class=\"file-ico file-{0}-lg\"></i></span>".FormatWith(e.Type) +
                                       "<span class=\"accessory-name\">{0}</span>".FormatWith(e.FileName) +
                                       "</a>" +
                                       "</li>");
            });
            htmlBuilder.AppendLine("</ul>");
            htmlBuilder.AppendLine("</div>");


            var result = htmlBuilder.Append(scriptBuilder);

            result = result.Replace("{name}",name);
            result = result.Replace("{accessoryCount}", accessoryCount.ToString());
            result = result.Replace("{batchAccessoryUrl}", urlHelper.Action("GetAccessoryZip", "Accessory",new{ids= ids ,zipName=zipName}));

            return new HtmlContentBuilder().AppendHtml(result.ToString());
        }
    }

    public enum AccessoryPanelStyle
    {
        /// <summary>
        /// 列表模式
        /// </summary>
        List,
        /// <summary>
        /// 平铺模式
        /// </summary>
        Tile

    }
}
